package com.huawei.dairy;

/**
 * @description:
 * @author: lhh
 * @time: 2022/6/7
 */


        import java.io.BufferedOutputStream;
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.io.FileOutputStream;
        import java.io.IOException;

public class FileUtil {
    public static final String SEPARATE_FIELD = "\n";
    public static final String SEPARATE_LINE = "\t";

    public FileUtil() {
    }

    public static void saveBooks(Diary diary) {
        String name = "黑马日记本.txt";
        FileInputStream in = null;

        try {
            in = new FileInputStream(name);
            if (in != null) {
                in.close();
                createFile(name, true, diary);
            }
        } catch (FileNotFoundException var4) {
            createFile(name, false, diary);
        } catch (IOException var5) {
            var5.printStackTrace();
        }

    }

    public static void createFile(String name, boolean label, Diary diary) {
        BufferedOutputStream out = null;
        StringBuffer sbf = new StringBuffer();

        try {
            String str;
            if (label) {
                out = new BufferedOutputStream(new FileOutputStream(name, true));
            } else {
                out = new BufferedOutputStream(new FileOutputStream(name));
                str = "欢迎来到黑马日记本";
                sbf.append(str).append("\n");
            }

            sbf.append("时间：").append(diary.time);
            sbf.append("姓名：").append(diary.name).append("\t");
            sbf.append("标题：").append(diary.title).append("\t");
            sbf.append("天气：").append(diary.weather).append("\n");
            sbf.append("内容：").append(diary.content).append("\n");
            str = sbf.toString();
            byte[] b = str.getBytes();

            for(int i = 0; i < b.length; ++i) {
                out.write(b[i]);
            }
        } catch (Exception var16) {
            var16.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception var15) {
                var15.printStackTrace();
            }

        }

    }

    public static void readFile() throws Exception {
        FileInputStream in = new FileInputStream("黑马日记本.txt");
        byte[] b = new byte[in.available()];
        in.read(b);
        String str = new String(b);
        System.out.println(str);
        in.close();
    }
}
